//
//@File    :	8.6_strtref.cpp
//@Time    :	2021/08/15 12:29:34
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	using structure refereces
//

#include <iostream>
#include <string>

using namespace std;

struct free_throws
{
    string name;
    int made;
    int attempts;
    float percent;
};

void display(const free_throws &ft);
//显示结构体的内容,而不修改,所以使用从const引用参数
void set_pc(free_throws &ft);
//设置结构体里的percent成员
free_throws &accumulate(free_throws &target,const free_throws &source);
//将第二个结构体里的成员attemps和made数据添加到第一个结构体响应成员中
//只修改第一个结构,因此第一个参数为引用,第二个参数为const引用
//返回值为第一个参数target的引用
int main()
{
    free_throws one={"Ifelsa Branch",13,14};
    free_throws two={"Andor Kontt",10,16};
    free_throws three={"Minnie Max",7,9};
    free_throws four={"Whily Looper",5,9};
    free_throws five={"Long Long",6,14};
    free_throws team={"Throwgoods",0,0};
//未初始化
    free_throws dup;

    set_pc(one);
    display(one);
  
    accumulate(team,one);
    display(team);//将one加到team并显示

    display(accumulate(team,two));//将two加到team并显示

    accumulate(accumulate(team,three),four);
    display(team);//先将three,再将four加至team并显示

    dup=accumulate(team,five);//将team中的值复制到dup中
    cout<<"Displaying team:\n";
    display(team);
    cout<<"Displaying dup after assignment:\n";
    display(dup);
    
    set_pc(four);
    accumulate(dup,five)=four;
    //函数返回值是一个引用,所以可行.等同于
    //accumulate(dup,five);
    //dup=four;//先将five加至dup中,后对dup重新赋值为four
    cout<<"Displaying dup after ill-advised assignment:\n";
    display(dup);

    system("pause");
    return 0;
}
void display(const free_throws &ft)
{
    cout<<"Name: "<<ft.name<<'\n';
    cout<<" Made: "<<ft.made<<'\t';
    cout<<"Attenmpts: "<<ft.attempts<<'t';
    cout<<"Percent: "<<ft.percent<<'\n';
}
void set_pc(free_throws &ft)
{
    if (ft.attempts!=0)
    {
        ft.percent=100.0f*float(ft.made)/float(ft.attempts);
    }
    else
    {
        ft.percent=0;
    }
}
free_throws &accumulate(free_throws &target,const free_throws &source)
{
    target.attempts+=source.attempts;
    target.made+=source.made;
    set_pc(target);
    return target;
}